package com.bw.services.device

import android.os.Bundle
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import com.bw.services.R
import com.therouter.router.Route
import dagger.hilt.android.AndroidEntryPoint
import android.widget.ImageView
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.LinearLayoutManager
import com.bw.common.utils.LogUtil
import com.bw.lib_base.base.BaseActivity
import com.bw.services.databinding.ActivityDeviceBinding
import com.bw.services.device.adapter.DeviceAdapter
import com.bw.services.device.model.DeviceEntity
import com.bw.services.device.viewmodel.DeviceViewModel
import com.therouter.TheRouter
import kotlinx.coroutines.launch

@AndroidEntryPoint
@Route(path = "app/DeviceActivity")
class DeviceActivity : BaseActivity<ActivityDeviceBinding,DeviceViewModel>() {
    lateinit var adapter: DeviceAdapter
    var list:DeviceEntity = DeviceEntity()
    override fun generatePremission(pageName: String): Array<String> {
        TODO("Not yet implemented")
    }

    override fun setupUI() {
        // 新增：返回按钮点击事件
        binding.ivBack.setOnClickListener {
            finish()
        }
        //跳转新增设备页面
        binding.ivMore.setOnClickListener {
            TheRouter.build("app/AddDeviceActivity").navigation()
        }
    }

    override fun fetchData() {
        vm.getDevice()
        adapter = DeviceAdapter(list)
        binding.rvDeviceList.adapter = adapter
        binding.rvDeviceList.layoutManager = LinearLayoutManager(this)

    }

    override fun observeData() {
        lifecycleScope.launch {
            vm.devicesData.collect{
                LogUtil.d("device",it.toString())
                if (it != null) {
                    list.clear()
                    list.addAll(it)
                    adapter.notifyDataSetChanged()
                }
            }
        }
    }

    override fun handleEvent() {

    }

    override val mLayoutResId: Int
        get() = TODO("Not yet implemented")
}